package com.tstudy.laoshibang.community;

import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.album.BitmapCache;
import com.tstudy.laoshibang.album.BitmapCache.ImageCallback;
import com.tstudy.laoshibang.album.ImageBucket;
import com.tstudy.laoshibang.base.BaseApplication;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 评论页adapter
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年6月3日 上午11:24:26
 */
public class AlbumListAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<ImageBucket> mItems;
	BitmapCache cache;
	ImageCallback callback = new ImageCallback() {
		@Override
		public void imageLoad(ImageView imageView, Bitmap bitmap,
				Object... params) {
			if (imageView != null && bitmap != null) {
				String url = (String) params[0];
				if (url != null && url.equals((String) imageView.getTag())) {
					((ImageView) imageView).setImageBitmap(bitmap);
				} else {
				}
			} else {
			}
		}
	};
	
	public AlbumListAdapter(BitmapCache cache) {
		 mInflater = LayoutInflater.from(BaseApplication.mContext);
		 this.cache = cache;
	}
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	
	public void setData(List<ImageBucket> items) {
		setData(items,true);
	}
	public void setData(List<ImageBucket> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<ImageBucket> getData() {
		return mItems;
	}
	

	@Override
	public Object getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if(convertView ==  null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.album_pop_item, null);
			viewHolder.cover = (ImageView)convertView.findViewById(R.id.album_pop_item_cover);
			viewHolder.name = (TextView)convertView.findViewById(R.id.album_pop_item_name);
			viewHolder.count = (TextView)convertView.findViewById(R.id.album_pop_item_count);
			viewHolder.selectIcon = (ImageView) convertView.findViewById(R.id.album_pop_item_select_icon);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(viewHolder != null){
			setConvertView(viewHolder,position,convertView);
		}
//		convertView.setOnClickListener(null);
		return convertView;
	}
	
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final ImageBucket bucket = (ImageBucket) getItem(position);
		viewHolder.name.setText(bucket.bucketName);
		viewHolder.count.setText(bucket.count+"张");
		viewHolder.selectIcon.setVisibility(bucket.isSelected?View.VISIBLE:View.GONE);
		cache.displayBmp(viewHolder.cover, bucket.imageList.get(0).thumbnailPath, bucket.imageList.get(0).imagePath, callback);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActionCallBack != null) {
					mActionCallBack.onItemClick(bucket, position);
				}
			}
		});
	}
	
	static class ViewHolder{
		ImageView cover;
		TextView name;
		TextView count;
		ImageView selectIcon;
	}
	
	public interface AdapterCallBack{
		public void onItemClick(ImageBucket bucket,int position);
	}
	AdapterCallBack mActionCallBack;
	public void setAdatperCallBack(AdapterCallBack actionCallBack){
		mActionCallBack = actionCallBack;
	}
}
